﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.Collections;
namespace BallData
{
    /// <summary>
    /// MatchOddsWnd.xaml 的交互逻辑
    /// </summary>
    public partial class MatchOddsWnd : Window
    {
        public MatchData matchdata;
        private MatchOddsData matchoddsdata;
        private String mId;
        public MatchOddsWnd()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            String content = (String)button.Content;
            foreach (String company in OddsUntil.companyList.Values)
            {
                String[] companyinfodata = company.Split(OddsUntil.SplitColumn);

                if (companyinfodata[2] == content)
                {

                    ShowMatchOdds(companyinfodata[0], mId);
                }
                
                
            }

        }
        public void InitData(MatchData matchdata,String mId)
        {

            this.mId = mId;
            this.matchdata = matchdata;

            MatchItem matchitem = (MatchItem)matchdata.Matchs[mId];

            if (matchitem != null)
            {
                StringBuilder strbuilder = new StringBuilder();
                strbuilder.Append("http://odds.gooooal.com/match/");
                strbuilder.Append(Convert.ToInt32(mId) / 1000);
                strbuilder.Append("/");
                strbuilder.Append(mId + ".match");
                strbuilder.Append("?" + OddsUntil.GetRandStr(10));


                HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(strbuilder.ToString());

                httprequest.ContentType = "text/html; charset=utf-8";

                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)httprequest.GetResponse();
                }
                catch (System.Exception ex)
                {
                    Console.Write(ex.Message);
                    return;
                }

                

                StreamReader sr = new StreamReader(response.GetResponseStream());
                // 得到赛事数据
                String szmatchdata = sr.ReadToEnd();
                sr.Close();
                response.Close();

                matchoddsdata = new MatchOddsData();

                //分隔大数据域
                String[] domains = szmatchdata.Split(OddsUntil.SplitDomain);

                //处理公共数据域
                String[] publicDomain = domains[0].Split(OddsUntil.SplitColumn);
                matchoddsdata.TimeStamp = Convert.ToUInt64(publicDomain[0], 10);
                matchoddsdata.LoadChg = Convert.ToInt32(publicDomain[1], 10);

                //处理比赛数据
                matchoddsdata.Match = new MatchItem(domains[1]);

                //var companyItem, oddsItem, companyList, oddsList;

                //处理公司数据
                String[] companyDomain = domains[2].Split(OddsUntil.SplitRecord);
                for (int i = 0; i < companyDomain.Length; i++)
                {
                    if (companyDomain[i].Length > 5)
                    {
                        CompanyItem companyItem = new CompanyItem(companyDomain[i]);
                        if (companyItem.cId == "201") continue;
                        matchoddsdata.CompanyList.Add(companyItem.cId, companyItem);
                    }
                }

                Hashtable companyList = null;
                List<OddsInfo_d> oddsList = null;
                //亚盘,欧盘,大小球
                String[] oddsDomain = domains[3].Split(OddsUntil.SplitRecord);
                for (int i = 0; i < oddsDomain.Length; i++)
                {
                    if (oddsDomain[i].Length > 5)
                    {
                        OddsInfo_d oddsItem = new OddsInfo_d(oddsDomain[i]);
                        if (oddsItem.cId == "201") continue;
                        if (oddsItem.oType == "a")
                            companyList = (Hashtable)matchoddsdata.AsianData[oddsItem.cId];
                        else if (oddsItem.oType == "e")
                            companyList = (Hashtable)matchoddsdata.EuropeData[oddsItem.cId];
                        else if (oddsItem.oType == "o")
                            companyList = (Hashtable)matchoddsdata.OverUnderData[oddsItem.cId];
                        else
                            continue;
                        if (companyList == null)
                        {
                            companyList = new Hashtable();
                            companyList["cId"] = oddsItem.cId;
                            if (oddsItem.oType == "a")
                                matchoddsdata.AsianData.Add(oddsItem.cId, companyList);
                            else if (oddsItem.oType == "e")
                                matchoddsdata.EuropeData.Add(oddsItem.cId, companyList);
                            else if (oddsItem.oType == "o")
                                matchoddsdata.OverUnderData.Add(oddsItem.cId, companyList);
                        }
                        oddsList = (List<OddsInfo_d>)companyList[oddsItem.sId];
                        if (oddsList == null)
                        {
                            oddsList = new List<OddsInfo_d>();
                            companyList.Add(oddsItem.sId, oddsList);
                        }
                        oddsList.Add(oddsItem);
                    }
                }


                ShowMatchOdds("1", mId);

            }
           
        }
        public void ShowMatchOdds(String cId,String mId)
        {
            MatchItem matchitem = (MatchItem)matchdata.Matchs[mId];

            if (matchitem != null)
            {
                LeagueItem leagueitem = (LeagueItem)matchdata.LeagueList[matchitem.lId];

                this.match_tile.Content = matchitem.t1CnName + " vs " + matchitem.t2CnName + "(" + leagueitem.cnName + ")";

                String[] matchoddsindex = { "1", "2", "3", "4" };

                this.company_oddsdata.Items.Clear();

                // 亚洲盘
                Hashtable cdata = (Hashtable)matchoddsdata.AsianData[cId];
                if (cdata != null)
                {
                    
                    foreach (String index in matchoddsindex)
                    {
                        List<OddsInfo_d> oddsinfolist = (List<OddsInfo_d>)cdata[index];
                        if (oddsinfolist != null)
                        {
                            TabItem tb = new TabItem();

                            if (cdata.Keys.Count - 1 > 1)
                                tb.Header = "亚盘" + index;
                            else
                                tb.Header = "亚盘";

                            ListView listview = new ListView();

                            GridView asiaview = new GridView();

                            asiaview.Columns.Add(new GridViewColumn { Header = "主队帖水", DisplayMemberBinding = new Binding("host") ,Width=80});
                            asiaview.Columns.Add(new GridViewColumn { Header = "盘口", DisplayMemberBinding = new Binding("vnname"), Width = 120 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "客队贴水", DisplayMemberBinding = new Binding("visit"), Width = 80 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "更新时间", DisplayMemberBinding = new Binding("updatetime"), Width = 180 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "备注", DisplayMemberBinding = new Binding("period"), Width = 80 });

                            oddsinfolist[0].period = "当前赔率";

                            listview.View = asiaview;

                            tb.Content = listview;
                            listview.ItemsSource = oddsinfolist;

                            this.company_oddsdata.Items.Add(tb);

                        }
                    }
                }

                // 欧洲盘
                cdata = (Hashtable)matchoddsdata.EuropeData[cId];
                if (cdata != null)
                {
                    foreach (String index in matchoddsindex)
                    {
                        List<OddsInfo_d> oddsinfolist = (List<OddsInfo_d>)cdata[index];
                        if (oddsinfolist != null)
                        {
                            TabItem tb = new TabItem();

                            if (cdata.Keys.Count - 1 > 1)
                                tb.Header = "欧盘" + index;
                            else
                                tb.Header = "欧盘";

                            ListView listview = new ListView();

                            GridView asiaview = new GridView();

                            asiaview.Columns.Add(new GridViewColumn { Header = "赔率", DisplayMemberBinding = new Binding("oddsdata1"), Width = 180 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "返还率", DisplayMemberBinding = new Binding("oddsdata2"), Width = 80 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "概率", DisplayMemberBinding = new Binding("oddsdata3"), Width = 180 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "更新时间", DisplayMemberBinding = new Binding("updatetime"), Width = 180 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "备注", DisplayMemberBinding = new Binding("period"), Width = 80 });

                            oddsinfolist[0].period = "当前赔率";

                            listview.View = asiaview;

                            tb.Content = listview;
                            listview.ItemsSource = oddsinfolist;

                            this.company_oddsdata.Items.Add(tb);
                        }
                    }
                }

                // 大小球
                cdata = (Hashtable)matchoddsdata.OverUnderData[cId];
                if (cdata != null)
                {
                    foreach (String index in matchoddsindex)
                    {
                        List<OddsInfo_d> oddsinfolist = (List<OddsInfo_d>)cdata[index];
                        if (oddsinfolist != null)
                        {
                            TabItem tb = new TabItem();

                            if (cdata.Keys.Count - 1 > 1)
                                tb.Header = "大小" + index;
                            else
                                tb.Header = "大小";

                            ListView listview = new ListView();

                            GridView asiaview = new GridView();

                            asiaview.Columns.Add(new GridViewColumn { Header = "大球贴水", DisplayMemberBinding = new Binding("host"), Width = 80 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "盘口", DisplayMemberBinding = new Binding("vnname"), Width = 120 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "小球贴水", DisplayMemberBinding = new Binding("visit"), Width = 80 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "更新时间", DisplayMemberBinding = new Binding("updatetime"), Width = 180 });
                            asiaview.Columns.Add(new GridViewColumn { Header = "备注", DisplayMemberBinding = new Binding("period"), Width = 80 });

                            oddsinfolist[0].period = "当前赔率";

                            listview.View = asiaview;

                            tb.Content = listview;
                            listview.ItemsSource = oddsinfolist;
                            this.company_oddsdata.Items.Add(tb);
                        }
                    }
                }

            }
            
           
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Maximized;

            foreach (String company in OddsUntil.companyList.Values)
            {
                Button button = new Button();
                button.Content = company.Split(OddsUntil.SplitColumn)[2];
                button.HorizontalAlignment = HorizontalAlignment.Left;
                button.Margin = new Thickness(5);
                button.Height = 57;
                button.Width = 57;
                button.Click += Button_Click;
                this.company_buttons.Children.Add(button);
            }
        }
    }
}
